package com.douwong.student.base;

import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.douwong.student.R;
import com.douwong.student.activity.ClassTestActivity;
import com.douwong.student.model.ResTestProblemsInfo;
import com.douwong.student.popuwindow.ShowSubjectDetailPopuWindow;
import com.douwong.student.utils.ImagFilterUtil;
import com.douwong.student.utils.LogUtils;
import com.douwong.student.utils.UIUtils;
import com.douwong.student.view.MTextView;

import java.util.List;

/**
 * @autor yichao
 * @time 2017/4/515:34
 * @des 选择题基类
 */
public abstract class BaseChoiceFragment extends BaseFragment {
    private static final String TAG = "BaseChoiceFragment";
    protected ClassTestActivity mClassTestActivity;
    protected ResTestProblemsInfo.TopicItemBean mTopicItemBean;
    protected int[] linearlayouts = new int[] { R.id.ll_A_choice, R.id.ll_B_choice, R.id.ll_C_choice, R.id.ll_D_choice,
            R.id.ll_E_choice, R.id.ll_F_choice, R.id.ll_G_choice, R.id.ll_H_choice };
    protected int[] textViews = new int[] { R.id.tv_choose_A, R.id.tv_choose_B, R.id.tv_choose_c, R.id.tv_choose_D,
            R.id.tv_choose_e, R.id.tv_choose_F, R.id.tv_choose_g, R.id.tv_choose_H};
    protected String[] chart = { "A", "B", "C", "D", "E", "F", "G", "H" };
    private ImageView mIvHead;
    private MTextView mMtTopic;
    private LinearLayout mLlTitleRoot;
    protected View mContenView;
    private ShowSubjectDetailPopuWindow mPoDetailPopu;

    public BaseChoiceFragment(ClassTestActivity classTestActivity, ResTestProblemsInfo.TopicItemBean topicItemBean) {
        super();
        mClassTestActivity = classTestActivity;
        mTopicItemBean = topicItemBean;
    }

    @Override
    protected View getRootView(LayoutInflater inflater, ViewGroup container) {
        mContenView = UIUtils.inflate(getActivity(), R.layout.choice_questions);

        mIvHead = (ImageView) mContenView.findViewById(R.id.iv_answer_show_pic);
        mMtTopic = (MTextView) mContenView.findViewById(R.id.tv_question_choice);
        mLlTitleRoot = (LinearLayout) mContenView.findViewById(R.id.ll_choicefragme_title_root);
        return mContenView;
    }

    @Override
    protected void initData() {
        super.initData();
        boolean showIcon = isShowIcon( mTopicItemBean);
        if (showIcon){
            mIvHead.setVisibility(View.VISIBLE);
        }
        //题目数据
        ImagFilterUtil.filterMTImage(mMtTopic,mTopicItemBean.getBody());
        //选项
        for (int i = 0; i < mTopicItemBean.getOptions().size(); i++) {
            LinearLayout linearLayoutView = (LinearLayout) mContenView.findViewById(linearlayouts[i]);
            linearLayoutView.setVisibility(View.VISIBLE);
            ImagFilterUtil.filterImage((TextView) mContenView.findViewById(textViews[i]),mTopicItemBean.getOptions().get(i).getValue(),1);
        }

    }

    @Override
    protected void initEvent() {
        super.initEvent();
        for (int i = 0; i < linearlayouts.length; i++) {
            mContenView.findViewById(linearlayouts[i]).setOnClickListener(mOnClickListener);
        }
        mLlTitleRoot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //
                mPoDetailPopu = new ShowSubjectDetailPopuWindow(mClassTestActivity,
                        mTopicItemBean  );
                mPoDetailPopu.showAtLocation(getView(), Gravity.CENTER, 0, 0);

            }
        });
    }
    View.OnClickListener mOnClickListener=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.ll_A_choice:
                    setSlectViewState("A");
                    break;
                case R.id.ll_B_choice:
                    setSlectViewState("B");
                    break;
                case R.id.ll_C_choice:
                    setSlectViewState("C");
                    break;
                case R.id.ll_D_choice:
                    setSlectViewState("D");
                    break;
                case R.id.ll_E_choice:
                    setSlectViewState("E");
                    break;
                case R.id.ll_F_choice:
                    setSlectViewState("F");
                    break;
                case R.id.ll_G_choice:
                    setSlectViewState("G");
                    break;
                case R.id.ll_H_choice:
                    setSlectViewState("H");
                    break;
            }

        }
    };


    /**
     * 判断是否显示标题图片
     * @param mQditem
     */
    private boolean isShowIcon(ResTestProblemsInfo.TopicItemBean mQditem) {
        String itemBody = mQditem.getBody();//题目
        List<String> mTitlData = ImagFilterUtil.filterUrl(itemBody);
        for (int i = 0; i < mQditem.getOptions().size(); i++) {
            String answerValue = mQditem.getOptions().get(i).getValue();
            if (answerValue!=null){
                List<String> list = ImagFilterUtil.filterUrl(answerValue);
                if (list.size()>0){
                    for (String url : list) {
                        mTitlData.add(url);
                        LogUtils.i(TAG,"isshowIcon+++++++++++++++"+url);

                    }
                }

            }
        }

        if (mTitlData.size()>0){
            return true;
        }
        return false;
    }
    /**
     * 选项点击后的状态
     * @param str
     */
    protected abstract void setSlectViewState(String str);

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mPoDetailPopu!=null && mPoDetailPopu.isShowing()){
            mPoDetailPopu.dismiss();
        }
    }
}
